$(function () {
    onload();
    $('#side-menu ul').eq(3).addClass('in');
    $('#btn_search').on('click',function(){
        theLocation();
    });
});


// 百度地图API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.364388, 37.444522);

var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});// 左下角，添加比例尺
var top_right_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}); //右下角，仅包含平移和缩放按钮
map.addControl(top_left_control);
map.addControl(top_right_navigation);

map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用
map.centerAndZoom(point, 5);

map.enableInertialDragging();
var size = new BMap.Size(10, 20);
map.addControl(new BMap.CityListControl({
    anchor: BMAP_ANCHOR_TOP_LEFT,
    offset: size,
}));

var array=[];
//用经纬度设置地图中心点   查询用户
function theLocation(){
    $.ajax({
        url:whole_path+'/api/location/list',
        type:'get',
        data:$('#location_form').serialize(),
        dataType:'json',
        success:function(data){
            if(data.status == 0) {
                var locationList = data.data;
                //查询条件有值才跳动
                if (locationList != null && locationList.length == 1){
                    var lon =locationList[0].longitude;
                    var lat =locationList[0].latitude;
                    var new_point = new BMap.Point(lon, lat);
                    var marker = new BMap.Marker(new_point);  // 创建标注
                    if (array.length == 1) {
                        map.removeOverlay(array[0]);
                    }
                    array = [];
                    array.push(marker);
                    map.addOverlay(marker);              // 将标注添加到地图中
                    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                    map.panTo(new_point);
                }else {
                    map.removeOverlay(array[0]);
                }
            }else {
                $.toast(data.msg,3000);
            }
        },
        error : function() {
            $.toast('服务器未响应,请稍候重试',5000);
        }

    });


}
function onload(){
    $.ajax({
        url:whole_path+'/api/location/list',
        type:'get',
        dataType:'json',
        success:function(data){
            var locationList = data.data;
            $.each(locationList,function(index,item) {
                var lon = item.longitude;
                var lat = item.latitude;
                var carStatus = item.carStatus;
                switch (carStatus){
                    case 0:
                        carStatus = '空车模式'
                        break;
                    case 1:
                        carStatus = '满载模式'
                        break;
                    case 2:
                        carStatus = '配货模式'
                        break;
                }
                var content = '帐号:'+item.userName+'</br>姓名:'+item.realName+'</br>登录时间:'+timestampFormat(item.loginTime)+'</br>车辆状态:'+carStatus+'</br>当前位置:'+item.locateAddress
                var point = new BMap.Point(lon, lat);
                addMarker(content,point);

            });
        },
        error : function() {
            $.toast('服务器未响应,请稍候重试',5000);
        }

    });
}

// 编写自定义函数,创建标注
function addMarker(content,point){
    var myIcon = new BMap.Icon("../../static/img/label3.png", new BMap.Size(30,35));
    var marker = new BMap.Marker(point,{icon:myIcon});
    map.addOverlay(marker);
    addClickHandler(content,marker);
}

//添加信息窗口
var opts = {
    width : 250,     // 信息窗口宽度
    height: 150,     // 信息窗口高度
    title : "信息窗口" , // 信息窗口标题
    enableMessage:true//设置允许信息窗发送短息
};
function addClickHandler(content,marker){
    marker.addEventListener("click",function(e){
        openInfo(content,e)}
    );
}
function openInfo(content,e){
    var p = e.target;
    var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
    var infoWindow = new BMap.InfoWindow(content,opts);  // 创建信息窗口对象
    map.openInfoWindow(infoWindow,point); //开启信息窗口
}